
cc.Class({
    extends: cc.Component,

    properties: {
     //子弹预制件
    bullet:cc.Prefab,
    //血条
    plane_hp:cc.ProgressBar,
    //战机血量
    HP:4000,
    //撞中飞机产生爆炸
    bang:cc.Prefab,

     //道具
     prop:[cc.Prefab],

     //僚机
     wing_plane:cc.Prefab,

     //背景
     bg:[cc.Node],
     //显示文本
     show_boss:cc.Node,
     //大boss
     big_boss:cc.Prefab,

     boss_picture:[cc.SpriteFrame],
    },

    

    onLoad () {
        window.Global.bullet_num = 1;
        //判断是否出现boss   让程序按照规定执行一次
        this.isshowboss = true;
        //设置血量
        this.node.hp = this.HP;
        //生成子弹
       this.schedule(this.Bullet_launch,window.Global.bulletData[window.Global.bullet_num].bullet_speed);     
        //设置点击移动         
        this.node.on("touchmove",this.planeMovetEnent,this);
        //this.schedule(this.stop,40);

         //生成道具 两种道具 一种加子弹数量，一种增加僚机
         this.schedule(this.show_prop,5);
    },
   //生成道具
    show_prop:function(){
       
        var num = 0.9;//Math.random();  
        if(num > 0.8){
            var prop2 = window.Global.getClassAndAdd(this.prop[1],"Prop",this.node.parent); 
            prop2.tClass.init(3); 
          return;
        }
        if(num > 0.5){
            var prop1 = window.Global.getClassAndAdd(this.prop[0],"Prop",this.node.parent); 
            prop1.tClass.init(2);
           
        }
                      
        
  
      },

    //战机移动方法
    planeMovetEnent:function(touch){
        
         var location = this.node.convertTouchToNodeSpaceAR(touch);                     
         var newposition = cc.p(this.node.x + location.x,this.node.y + location.y);
         this.node.setPosition(newposition);
    },

    //生成战机子弹
    Bullet_launch:function(){

   for(let i = 0;i < window.Global.bullet_num + 1;i++){
    
    var bullet1 = window.Global.getClassAndAdd(this.bullet,"Bullet",this.node.parent);
    var rotation = window.Global.bulletData[window.Global.bullet_num].initial_rotation + i * window.Global.bulletData[window.Global.bullet_num].deviant_rotation;
    bullet1.tClass.init(rotation,0);
    bullet1.tNode.setPosition(this.node.x + window.Global.bulletData[window.Global.bullet_num].initial + i * window.Global.bulletData[window.Global.bullet_num].deviant
        ,this.node.y);
    this.node.zIndex = bullet1.tNode.zIndex + 1;

    }
        
    },

    //碰撞开始
    onCollisionEnter:function(other,self){
       

        //撞到子弹
        if(other.node.group == "enemy"){
            //boss被打死之后不再掉血
            if(!window.Global.Bullet_hit){
                return;
             }

            if(other.node.type == 0){
                this.node.hp -= 100;
            }
            if(other.node.type == 1){
                this.node.hp -= 50;
            }
            //撞到激光
            if(other.node.type == 2){    

            this.node.hp  = 0;
            let bang = window.Global.getClassAndAdd(this.bang,"Bang",self.node.parent);    
            bang.tNode.setPosition(other.node.position);
            return;
            }
            if(other.node.type == 3 || other.node.type == 4){
        
                this.node.hp -= 100;  
            }
            //播放爆炸动画
            let bang = window.Global.getClassAndAdd(this.bang,"Bang",self.node.parent);    
            bang.tNode.setPosition(other.node.position);
            other.node.destroy();
        }
         //撞到陨石或道具
        if(other.node.group == "Meteorite"){
        
            //撞到陨石
            if(other.node.type == 0){
                this.node.hp -= 50;
            //播放爆炸动画
            let bang = window.Global.getClassAndAdd(this.bang,"Bang",self.node.parent);    
            bang.tNode.setPosition(other.node.position);
            }
            if(other.node.type == 1){
                this.node.hp -= 50;
            //播放爆炸动画
            let bang = window.Global.getClassAndAdd(this.bang,"Bang",self.node.parent);    
            bang.tNode.setPosition(other.node.position);
            }

            //如果撞到道具
            if(other.node.type == 2){
            window.Global.bullet_num++;
            if(window.Global.bullet_num > 3){
                window.Global.bullet_num = 3;
            }
    
            }
            if(other.node.type == 3){            
            //判断是否有僚机，如果有，就判断僚机在哪一边
            if(this.node.children.length == 1){
        
                let wing_plane = window.Global.getClassAndAdd(this.wing_plane,"Wing_plane",this.node); 
                wing_plane.tNode.runAction(cc.moveBy(1,cc.p(-60,-10)));
                wing_plane.tClass.init(-60,0);
                
            }else if(this.node.children.length == 2){
                    let wing_plane = window.Global.getClassAndAdd(this.wing_plane,"Wing_plane",this.node);
                    if(this.node.children[1].type == 0){
                        wing_plane.tNode.runAction(cc.moveBy(1,cc.p(60,-10))); 
                        wing_plane.tClass.init(60,1);
                    }else{
                        wing_plane.tNode.runAction(cc.moveBy(1,cc.p(-60,-10))); 
                        wing_plane.tClass.init(-60,0);
                    }
                        
                }
                
            }
            other.node.destroy();
        }
        
         //撞到敌机
        if(other.node.group == "boss"){
            other.node.destroy();
            let bang = window.Global.getClassAndAdd(this.bang,"Bang",self.node.parent.parent); 
            bang.tNode.setPosition(other.node.position);
            this.node.hp  -= 200;
            if(this.node.hp <= 0){
                self.node.destroy();
            }
           
        }
        

    },

    //出现boss
    show_bigboss:function(){
         //停止生成战机子弹
        window.Global.bullet_num = -1;
        //停止生成僚机子弹
        if(this.node.children.length == 2){
            this.node.children[1].getComponent("Wing_plane").stop_winplane_schedule();
        }else if(this.node.children.length == 3){
            this.node.children[1].getComponent("Wing_plane").stop_winplane_schedule();
            this.node.children[2].getComponent("Wing_plane").stop_winplane_schedule();
        }

             
        //实例化boss预制件
        let rand = parseInt(Math.random()*this.boss_picture.length);
        this.bigboss = window.Global.getClassAndAdd(this.big_boss,"Bigboss",this.node.parent);    
        this.bigboss.tNode.getComponent(cc.Sprite).spriteFrame = this.boss_picture[rand];
        this.bigboss.tNode.setPosition(cc.p(0,this.node.parent.height / 2 + this.bigboss.tNode.height / 2));
        
        var callfun = cc.callFunc(function(){
            //当boss运动到指定位置之后，就开始生成战机子弹和僚机子弹
            this.bigboss.tClass.init(); 

            window.Global.bullet_num = 1;
            if(this.node.children.length == 2){
                this.node.children[1].getComponent("Wing_plane").begin_winplace_schedule();
            }else if(this.node.children.length == 3){
                this.node.children[1].getComponent("Wing_plane").begin_winplace_schedule();
                this.node.children[2].getComponent("Wing_plane").begin_winplace_schedule();
            }

        }.bind(this));
        var action = cc.moveBy(3,cc.p(0,-this.bigboss.tNode.height));
       this.bigboss.tNode.runAction(cc.sequence(action,callfun));
       
    },

    //出现boss
    Appear_boss:function(){
             
        //停止生成敌机和道具
        this.node.parent.getComponent("Game").stop_schedule();
        this.unschedule(this.show_prop);
        //屏幕闪烁
        var seq = cc.repeat(
            cc.sequence(
                cc.tintTo(1,231,91,142),
                cc.tintTo(1,255,255,255)
            ), 5);

        var seq1 = cc.repeat(
            cc.sequence(
                cc.tintTo(1,231,91,142),
                cc.tintTo(1,255,255,255)
            ), 5);

        this.bg[0].runAction(seq);
        this.bg[1].runAction(seq1);
        //播放文本动画
        this.show_boss.getComponent(cc.Animation).play();
       
        this.scheduleOnce(this.show_bigboss,8);

    },

    show_enemy_plane:function(){
       
        window.Global.Bullet_hit = true;

        this.node.parent.getComponent("Game").begin_schedule();
        //生成道具 两种道具 一种加子弹数量，一种增加僚机
        this.schedule(this.show_prop,5);
    },

    update (dt) {
         //刷新血条
         this.plane_hp.getComponent(cc.ProgressBar).progress = this.node.hp / this.HP;

        //分数达到一定数字就出现boss
        if(window.Global.Stage_score >= 3000){
            window.Global.Stage_score = 0;
            this.Appear_boss();
        }  
     
        if(window.Global.isstartgame){          
            window.Global.isstartgame = false;
            this.scheduleOnce(this.show_enemy_plane,4);
        }
  
        
    },
});
